package com.hilinos.base.common.init;

import javax.servlet.http.HttpServletRequest;

import com.hilinos.base.service.KeyInfoService;
import com.hilinos.base.service.UserInfoService;
import com.hilinos.base.user.entity.pojo.UserInfoPoJo;
import com.hilinos.common.common.handler.ITokenHandler;
import com.hilinos.common.common.handler.NoTokenEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;

@Order(10001)
@Component
public class MvcInit implements ITokenHandler {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private KeyInfoService keyInfoService;

    @Override
    public boolean checkToken(String token, HttpServletRequest request, HandlerMethod method, NoTokenEntity noTokenEntity) {
        UserInfoPoJo userInfo = userInfoService.checkToken(token);

        if (noTokenEntity.isOnlySuperAdmin()) {
            return keyInfoService.isAdmin(userInfo.getAppid());
        }

        return true;
    }
}
